// Copyright 2013 Petar Maymounkov
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ns

import (
	"fmt"
	"io"
	"io/ioutil"
	"reflect"
)

// RequestReader
type RequestReader struct {
	io.Reader
	//intr chan struct{}
}

var typeOfRequestReader = reflect.TypeOf(&RequestReader{})

func (q *RequestReader) Read(p []byte) (int, error) {
	return q.Reader.Read(p)
}

func (q *RequestReader) Close() error {
	return nil
}

func (q *RequestReader) ReadSlice() []byte {
	all, err := ioutil.ReadAll(q.Reader)
	if err != nil {
		panic(err)
	}
	return all
}

func (q *RequestReader) ReadString() string {
	all, err := ioutil.ReadAll(q.Reader)
	if err != nil {
		panic(err)
	}
	return string(all)
}

// ResponseWriter
type ResponseWriter struct {
	io.WriteCloser
}

var typeOfResponseWriter = reflect.TypeOf(&ResponseWriter{})

func (p *ResponseWriter) WriteSlice(s []byte) {
	defer p.WriteCloser.Close()
	p.WriteCloser.Write(s)
}

func (p *ResponseWriter) WriteString(s string) {
	p.WriteCloser.Write([]byte(s))
}

func (p *ResponseWriter) Printf(format string, arg ...interface{}) {
	p.WriteString(fmt.Sprintf(format, arg...))
}

func (p *ResponseWriter) Errorf(format string, arg ...interface{}) {
	s := fmt.Sprintf(format, arg...)
	p.WriteString(s)
	panic(s)
}
